/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.alterac.blurkit.slice;

import io.alterac.blurkit.BlurLayout;
import io.alterac.blurkit.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;

/**
 * 主页面
 *
 * @since 2021-04-27
 */
public class MainAbilitySlice extends AbilitySlice {
    private float potiveY = 0;
    private final int num1500 = 1500;
    private final int num1000 = 1000;
    private final int num500 = 500;
    private AnimatorValue animatorValue;
    private boolean isShangAndXia = true;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        BlurLayout blurlayout = (BlurLayout) findComponentById(ResourceTable.Id_blurlayout);
        animatorValue = new AnimatorValue();
        startAnimation(blurlayout);
    }

    /**
     * 开始插值动画
     *
     * @param blurlayout
     */
    private void startAnimation(BlurLayout blurlayout) {
        getUITaskDispatcher().delayDispatch(new Runnable() {
            @Override
            public void run() {
                animationAction(blurlayout);
            }
        }, 1);
    }

    private void animationAction(BlurLayout blurlayout) {
        animatorValue.setDuration(num1500);
        animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue2, float v) {
                if (!isShangAndXia) {
                    goBackwards(v, blurlayout);
                } else {
                    positive(v, blurlayout);
                }
            }
        });
        animatorValue.setCurveType(Animator.CurveType.ACCELERATE_DECELERATE);
        animatorValue.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                int temp = num1000;
                if (temp == potiveY) {
                    isShangAndXia = false;
                } else {
                    isShangAndXia = true;
                }
                animatorValue.start();
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
        animatorValue.start();
    }

    private void positive(float v, BlurLayout blurlayout) {
        potiveY = (float) (((double)v * (double) num500) + (double)num500);
        blurlayout.setY(potiveY); // 更改组r件在父布局中的位置
        blurlayout.setContentPositionY(potiveY);
        blurlayout.invalidate();
    }

    private void goBackwards(float v, BlurLayout blurlayout) {
        potiveY = 0;
        potiveY = (float) ((double)num1000 - ((double)v * (double)num500));
        blurlayout.setY(potiveY);
        blurlayout.setContentPositionY(potiveY);
        blurlayout.invalidate();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
